package com.lhb.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhb.dao.CourseClassfiyDao;
import com.lhb.dao.CourseDao;
import com.lhb.pojo.BaseResp;
import com.lhb.pojo.Course;
import com.lhb.pojo.CourseClassfiy;
import com.lhb.pojo.rep.CourseReq;
import com.lhb.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.9 下午 18:52
 */

@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseDao courseDao;
    @Resource
    CourseClassfiyDao courseClassfiyDao;

    @Override
    public BaseResp findAll(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Course> all = courseDao.findAll();
        PageInfo<Course> of = PageInfo.of(all);
        BaseResp baseResp = new BaseResp();
        baseResp.setCount(of.getTotal());
        baseResp.setData(of.getList());
        baseResp.setCode(0);
        baseResp.setMsg("查询成功");
        return baseResp;
    }

    @Override
    public int del(Integer id) {
        return courseDao.del(id);
    }

    @Override
    public BaseResp insertCourse(CourseReq courseReq) {
        //新增课程
        Course course = new Course();
        BeanUtils.copyProperties(courseReq, course);
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        int i = courseDao.insertCourse(course);
        System.out.println(course.getId());
        //向中间表新增课程与课程分类的关联关系
        CourseClassfiy courseClassfiy = new CourseClassfiy();

        courseClassfiy.setCourseId(course.getId());

        courseClassfiy.setClassifyId(courseReq.getCourseClassfiy());

        courseClassfiyDao.insertCourseClassfiy(courseClassfiy);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("添加成功");
        return baseResp;
    }

    @Override
    public BaseResp updateCourser(Course course) {
        int i = courseDao.updateCourse(course);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("修改成功");
        return baseResp;
    }


}
